var activeTicket = null;

function fetchFeeds (feedUrl) {
	var authString = "http://" + user + ":" + pass + "@";
	var authMatch = /http:\/\//g;
	feedUrl = feedUrl.replace(authMatch,authString);
	var gzUrl = "http://api.grazr.com/xdgraze?xid=0&rnd="+Math.round(Math.random()*1000)+"&url="+feedUrl;
	/*jsoncallback=? required for XSS */
	$.getJSON(gzUrl+"&jsoncallback=?");
}

/* Feed return */
function xdReceive (xid,data) {
	var head = data.opml.childNodes[0].head;
	var body = data.opml.childNodes[1].body;

	if (head.childNodes[0].title.nodeValue.indexOf("Ticket #") != "-1") {
		if (body.childNodes) {
			$.each(body.childNodes, function(i,node) {
				processComments(node.outline);
			});
		}
	} else {
		$.each(body.childNodes, function(i,node) {
			createTicket(node.outline);
		});
	}
}

function createTicket (outline) {
	/* Trac gives verbose outlines as three children, in addition to the main attribute:
			1.  Title.  2.  Long description  3.  A random closing tag. */
		var title = unescape(outline.childNodes[0].outline.attribute.text);
		var htmlUrl = outline.childNodes[0].outline.attribute.htmlUrl;
		var longdesc = unescape(outline.childNodes[1].outline.attribute.text);
			
	var ticket = $("<div class=\"ticket\"><div class=\"ticket_title\">" + title + 
				 " <a class=\"permalink\" href=\"#\">&raquo;</a></div><div class=\"ticket_desc\">" + longdesc + 
				 "<p class=\"comments_title\">View Ticket Activity</p><div class=\"comments_body\"></div></div></div>").appendTo("#body").slideDown("slow");
	
	
	$(".permalink").bind("click",function(event) {
		document.location.href = htmlUrl;
		event.stopPropagation();
	});
		
	$(".comments_title:last").bind("click",function(event) {
		/* Fetch comments for this node */
		fetchComments(htmlUrl+"?format=rss");
		event.stopPropagation();
	});
	
	$(ticket).bind("mouseover",function() {
		this.style.backgroundColor = "#96d0f9";
	});
	
	$(ticket).bind("mouseout",function() {
		this.style.backgroundColor = "#66ccff";
	});
	
	$(ticket).bind("click",function() {
		activeTicket = this;
		$(this.lastChild).slideToggle("normal");
	});
}

function fetchComments (ticketUrl) {
	var authString = "http://" + user + ":" + pass + "@";
	var authMatch = /http:\/\//g;
	ticketUrl = ticketUrl.replace(authMatch,authString);
	var gzUrl = "http://api.grazr.com/xdgraze?xid=0&rnd="+Math.round(Math.random()*1000)+"&url="+ticketUrl;
	/*jsoncallback=? required for XSS */
	$.getJSON(gzUrl+"&jsoncallback=?");
}

function processComments (outline) {
	/* Ticket data is returned among five children:  
		0: title. 1: date.  2:author. 3: content.  4.  postclear (unused); */
	var comment = [{title:""},{date:""},{author:""},{content:""}];
	comment.title =		outline.childNodes[0].outline.attribute.text;
	comment.date =	 	unescape(outline.childNodes[1].outline.attribute.text);
	comment.author = 	unescape(outline.childNodes[2].outline.attribute.text);
	comment.content = 	unescape(outline.childNodes[3].outline.attribute.text);
	var insertSpot = activeTicket.lastChild.lastChild;
	$("<div class=\"comments\">" + comment.content + "</div><div class=\"comments_info\">Posted on "+comment.date+comment.author+"</div>").appendTo(insertSpot).fadeIn();
}